package com.apes.fn.fico.invoiceApply.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductGroup;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.fico.invoiceApply.model
 * @ClassName: InvoiceApplyItem
 * @Author: pyb
 * @Description: 发票申请明细
 * @CreateTime: 2021-04-08 17:24
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InvoiceApplyItem.CONST_TABLE_NAME)
public class InvoiceApplyItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_invoice_apply_item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "发票申请明细")
    @Comment("明细编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "invoice_apply_id", referencedColumnName = "id")
    @Comment("发票申请")
    private InvoiceApply invoiceApply;

    @Column(name = "sales_organization", length = 36)
    @Comment("销售组织编码")
    private String salesOrganization;

    @Comment("结算单号")
    @Column(name = "settle_policy", length = 36)
    private String settlePolicy;

    @Column(name = "project", length = 30)
    @Comment("项目")
    private String project;

    @Column(name = "project_fee", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("项目费")
    private double projectFee = 0;

    @Column(name = "project_fee_received", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("已开票项目费")
    private double projectFeeReceived = 0;

    @Column(name = "project_fee_deducted", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("本次开票项目费")
    private double projectFeeDeducted = 0;

    @Column(name = "statement_id", length = 30)
    @Comment("对账单号")
    private String statementId;

    @Column(name = "origin", length = 30)
    @Comment("源单据")
    private String origin;

    @Column(name = "origin_business_type", length = 32)
    @Comment("源单据类型")
    private String originBusinessType;

    @Column(name = "source", length = 30)
    @Comment("来源单据")
    private String source;

    @Column(name = "source_business_type", length = 32)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product;


//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "serveType_id", referencedColumnName = "id")
//    @Comment("服务类型")
//    private ServeType serveType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_group", referencedColumnName = "id")
    private ProductGroup productGroup;

    @Transient
    private String classify;

    public String getClassify() {
        if (product == null) return null;
        if (product.getClassify() == null) return null;
        return product.getClassify().getName();
    }

    @Transient
    private String productName;

    public String getProductName() {
        if (product == null) return null;
        return product.getName();
    }


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位")
    private ProductUom uom;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("税率")
    private Tax tax;


    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("含税单价")
    private double priceUnit = 0;

    @Column(name = "product_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("数量")
    private double productQty = 0;

    @Column(name = "price_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总计")
    private double priceTotal = 0;


    @Transient
    private String originBusinessTypeName;

    public String getOriginBusinessTypeName() {
        return getBusinessTypeName("originBusinessType");
    }

    @Transient
    private String sourceBusinessTypeName;

    public String getSourceBusinessTypeName() {
        return getBusinessTypeName("sourceBusinessType");
    }
}
